package Retail;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.math.BigDecimal;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RetailProductUpdate extends HttpServlet{

	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req,resp);
	}

	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doProcess(req,resp);
	}
	
	protected void doProcess(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String tProductID= req.getParameter("ProductID");
		System.out.print(tProductID);
		String tProductDesc = req.getParameter("ProductDesc");
		BigDecimal tProductPrice= new BigDecimal(req.getParameter("ProductPrice"));
		BigDecimal tProductCost= new BigDecimal(req.getParameter("ProductCost"));
		BigDecimal tProductQuantity= new BigDecimal(req.getParameter("ProductQuantity"));
		int tProductVaildDate= Integer.parseInt(req.getParameter("ProductVaildDate"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date tProductEntryDate = null;
		try {
			tProductEntryDate = sdf.parse(req.getParameter("ProductEntryDate"));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		try {
			Class.forName("org.h2.Driver");
			Connection con = DriverManager.getConnection("jdbc:h2:~/retail",
					"sa", "");
			PreparedStatement ps = con.prepareStatement("Update PRODUCT set DESCRIPTION=(?),SOLD_PRICE =(?),COST_PRICE=(?),QUANTITY=(?),VALID_DATE=(?),WAREHOUSING_TIME=(?) Where PRODUCTID=(?)");
			ps.setString(7, tProductID);
			ps.setString(1, tProductDesc);
			ps.setBigDecimal(2, tProductPrice);
			ps.setBigDecimal(3, tProductCost);
			ps.setBigDecimal(4, tProductQuantity);
			ps.setInt(5, tProductVaildDate);
			ps.setDate(6, new java.sql.Date(tProductEntryDate.getTime()));
			
			
			 ps.executeUpdate();
			
			ps.close();
			con.close();

			resp.sendRedirect("Product/ProductMain.jsp");
	}catch (ClassNotFoundException e) {
		resp.sendRedirect("Product/ProductNotFound.jsp");
	} catch (SQLException e) {
		resp.sendRedirect("Product/ProductNotFound.jsp");
	}
	
		
	}

	
}
